/*
Guillaume COLLOMBET
*/

#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include "pixel.h"
#include "image.h"

void imInit(Image* image, int widht, int heigth) {
    image->pix = (Pixel*) malloc(sizeof(Pixel) * widht * heigth);
    image->dimx = widht;
    image->dimy = heigth;
}

Image* imCreer(int widht, int heigth) {
    Image* image;
    imInit(image, widht, heigth);
    return image;
}

void imLibere(Image* image) {
    free(image->pix);
    image->dimx = 0;
    image->dimy = 0;
}

const Pixel* getPix(const Image* image, int x, int y) {
    assert((x<=image->dimx) && (y<=image->dimy));
    return &image->pix[image->dimx * y + x];
}

void setPix(Image* image, int x, int y, const Pixel* pixel) {
    assert((x<=image->dimx) && (y<=image->dimy));
    Pixel* destPixel;
    destPixel = &image->pix[image->dimx * y + x];
    setR(destPixel,pixel->r);
    setV(destPixel,pixel->v);
    setB(destPixel,pixel->b);
}

void imEffacer(Image* image, const Pixel* pixel) {
    imDessineRect(image,0,0,image->dimx,image->dimy,pixel);
}

void imDessineRect(Image* image, int x, int y, int widht, int height, const Pixel* pixel) {
    //l'origine du rectangle est-elle dans l'image
    assert((x<=image->dimx) && (y<=image->dimy));
    //le rectangle est-il contenu dans l'image
    assert((x >= 0) && (y >= 0) && (widht <= image->dimx) && (height <= image->dimy));
    for (x = 0; x < widht; x++) {
        for (y = 0; y < height; y++) {
            setPix(image, x, y, pixel);
        }
    }
}

void imSauver(const Image *pIm, const char filename[])
{
    FILE* f;
	Pixel pix;
	int x,y;

    f = fopen(filename, "w");
    if (f==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", filename);
        assert( f );
    }
    fprintf( f , "P3\n");                           // P3 = ascii avec 3 composantes RVB
    fprintf( f , "%d %d\n", pIm->dimx, pIm->dimy);      // dimension de l'image
    fprintf( f , "255\n");                          // chaque composante est comprise entre 0 et 255

    for(y=0;y<pIm->dimy;y++)
    {
        for(x=0;x<pIm->dimx;x++)
        {
            pix = *getPix(pIm,x,y);
            fprintf(f, "%d %d %d  ", pix.r, pix.v, pix.b);
        }
    }
    printf("Sauvegarde de l'image %s ...OK\n", filename);
    fclose(f);
}

void imOuvrir(Image *pIm, const char filename[])
{
    FILE* f;
	Pixel pix;
	int x, y, dimx, dimy;

    f = fopen(filename, "r");
    if (f==NULL)
    {
        printf("Erreur lors de l'ouverture de %s\n", filename);
        assert( f );
    }

    assert( fscanf( f , "P3\n%d %d\n255\n", dimx, dimy) == 2);
    imInit( pIm, dimx, dimy);

    for(y=0;y<pIm->dimy;y++)
    {
        for(x=0;x<pIm->dimx;x++)
        {
            assert( fscanf(f, "%d %d %d  ", &pix.r, &pix.v, &pix.b)==3 );
			setPix(pIm,x,y,&pix);
        }
    }
    fclose(f);
    printf("Lecture de l'image %s ...OK\n", filename);
}

void imPrintf(const Image *pIm)
{
	int x,y;
	Pixel pix;

    printf( "%d %d\n", pIm->dimx, pIm->dimy);      // dimension de l'image

    for(y=0;y<pIm->dimy;++y)
    {
        for(x=0;x<pIm->dimx;++x)
        {
            pix = *getPix(pIm,x,y);
            printf("%d %d %d  ", pix.r, pix.v, pix.b);
        }
        printf("\n");
    }
}
